package com.server.pinggu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.pinggu.domain.SenStreets;
import com.server.pinggu.mapper.SenStreetsMapper;
import com.server.pinggu.service.ISenStreetsService;
import com.server.pinggu.vo.SenStreetsVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ltao
 * @date 2022-10-23
 */
@Service
public class SenStreetsServiceImpl extends ServiceImpl<SenStreetsMapper, SenStreets> implements ISenStreetsService {

    @Override
    public List<SenStreets> queryList(SenStreets senStreets) {
        LambdaQueryWrapper<SenStreets> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(SenStreets::getDel,0);
        List<SenStreets> list = this.list(objectLambdaQueryWrapper);
        Map<Long, List<SenStreets>> collect = list.stream().collect(Collectors.groupingBy(SenStreets::getPid));
        List<SenStreets> result = collect.get(0L);
        for (SenStreets streets : result) {
            List<SenStreets> value = collect.get(streets.getId());
            if (CollectionUtil.isNotEmpty(value)){
                streets.setList(value);
                streets.setSonNumber(value.size());
            }
        }
        return result;
    }

    @Override
    public List<SenStreetsVo> senStreetsVo(SenStreets senStreets) {
        LambdaQueryWrapper<SenStreets> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.ne(SenStreets::getId,-1);
        objectLambdaQueryWrapper.ne(SenStreets::getDel,1);
        List<SenStreets> list = this.list(objectLambdaQueryWrapper);
        List<SenStreetsVo> value = list.stream().map(a -> {
            SenStreetsVo senStreetsVo = new SenStreetsVo();
            senStreetsVo.setPid(a.getPid());
            senStreetsVo.setText(a.getTitle());
            senStreetsVo.setId(a.getId());
            return senStreetsVo;
        }).collect(Collectors.toList());
        Map<Long, List<SenStreetsVo>> collect = value.stream().collect(Collectors.groupingBy(SenStreetsVo::getPid));
        List<SenStreetsVo> result = collect.get(0L);
        for (SenStreetsVo streets : result) {
            List<SenStreetsVo> values =collect.get(streets.getId());
            if (CollectionUtil.isNotEmpty(values)){
                streets.setChildren(values);
            }else{
                streets.setChildren(new ArrayList<SenStreetsVo>());
            }
        }
        return result;
    }
}